package com.lr.server.demo3;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Request {
	
	private String method;
	
	private String url;
	
	private Map<String,List<String>> parmerters;
	
	private InputStream is;
	
	private String requestInfo;
	
	private static final String CRTL = "\r\n";
	
	public Request(){
		//避免空指针异常
		url = "";
		method = "";
		parmerters = new HashMap<String,List<String>>();		
	}

	
	public String getMethod() {
		return method;
	}

	public String getUrl() {
		return url;
	}

	public Map<String, List<String>> getParmerters() {
		return parmerters;
	}

	public Request(Socket socket){
		this();
		try {
			is = socket.getInputStream();
			byte[] data = new byte[20480];
			int len = is.read(data);
			if(len < 0){
				return;
			}
			requestInfo = new String(data,0,len);
		} catch (IOException e) {
			return;
		}
		parseRequestInfo();
	}
	
	public void parseRequestInfo(){
		if(null == requestInfo || requestInfo.trim().equals("")){
			return;
		}
		/*
		 * 分解字符串
		 */
		String firstLine = requestInfo.substring(0,requestInfo.indexOf(CRTL));
		int idx = firstLine.indexOf("/");
		method = firstLine.substring(0, idx).trim();
		String urlStr = firstLine.substring(idx,firstLine.indexOf("HTTP/")).trim();
		String params = "";
		if(this.method.equalsIgnoreCase("post")){
			this.url = urlStr;
			params = requestInfo.substring(requestInfo.lastIndexOf(CRTL) + CRTL.length());
		}
		else if(this.method.equalsIgnoreCase("get")){
			idx = urlStr.indexOf("?");
			if(idx < 0){
				return;    //不存在参数
			}
			this.url = urlStr.substring(0,idx);
			params = urlStr.substring(idx + 1);		
		}
		
		if(params.trim().equals("")){
			return;
		}
		//函数太长就分割
		parseParams(params);
	}
	
	public void parseParams(String params){
		String[] param = params.split("&");
		for(String temp : param){
			String[] keyAndValues = temp.split("=");
			if(keyAndValues.length == 1){
				parmerters.put(keyAndValues[0], null);
			}
			else{
			   List<String> values = new ArrayList<String>();
			   values.add(decode(keyAndValues[1]));
			   parmerters.put(keyAndValues[0], values);
		    }
		}
	}
	
	public String[] getParmerters(String key){
		List<String> list = parmerters.get(key);
		if(list == null){
			return null;
		}
		return list.toArray(new String[0]);
	}
	
	public String getParmerter(String key){
		String[] values = getParmerters(key);
		if(values == null){
			return null;
		}
		return values[0];
	}
	
	public String decode(String str,String enc){
		try {
			return URLDecoder.decode(str, enc);
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	
	public String decode(String str){
		try {
			return URLDecoder.decode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	
	public void close(){
		IOUtil.IOClose(is);
	}
}
